package com.itehima.Topic5;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    private static ArrayList<Good> goods = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        goods.add(new Good(LocalDateTime.of(2023,3,22,12,0,0,0),
                LocalDateTime.of(2023,3,22,12,10,0,0)
                ,"咸鸭蛋",10,22.8));
        goods.add(new Good(LocalDateTime.of(2023,3,22,13,0,0,0),
                LocalDateTime.of(2023,3,22,13,50,0,0),
                "摇头风扇",2,649));
        goods.add(new Good(LocalDateTime.of(2023,3,22,14,0,0,0),
                LocalDateTime.of(2023,3,22,14,10,0,0),
                "瓢虫项链",5,599));
        goods.add(new Good(LocalDateTime.of(2023,3,22,15,0,0,0),
                LocalDateTime.of(2023,3,22,15,10,0,0),
                "OPPO手机",1,2999));

        seckill();

    }

    private static void seckill() {
        System.out.println("===========欢迎进入JD秒杀页面============");
        System.out.println(LocalDateTime.now());
        while (true) {
            printGoodsInfo();
            System.out.println("选择需要秒杀的商品（输入商品序号）：");
            int goodId =sc.nextInt();
            if (goodId<1 ||goodId>goods.size()){
                System.out.println("你输入的商品Id不存在，请检查！");
            }else {
                if(isKillTime(goodId)){
                    if (!isSellOut(goodId)){
                        if (bugGood(goodId)){
                            System.out.println("恭喜您，秒杀成功！");
                        }else {
                            System.out.println("抱歉，"+goods.get(--goodId)+"被抢光了哦~");
                        }
                    }
                }
            }
        }

    }


    /*
    * 抢购一件商品
    * */
    private static boolean bugGood(int goodId) {
        int index = --goodId;
        if (goods.get(index).getStock()>=1){
            goods.get(index).setStock(goods.get(index).getStock()-1);
            return true;
        }
       return false;
    }


    /*
    * 判断商品是否全部卖光
    * */
    private static boolean isSellOut(int goodId) {
        int index = --goodId;
        if (goods.get(index).getStock() <= 0){
            System.out.println(goods.get(index).getName()+"已经被抢光了哦~");
        }else {
            return false;
        }
        return true;
    }


    /*
    * 判断商品是否在秒杀时间范围内
    * */
    private static boolean isKillTime(int goodId) {
        int index = --goodId;
        if (LocalDateTime.now().isAfter(goods.get(index).getStart())
                && LocalDateTime.now().isBefore(goods.get(index).getEnd())){
            return true;
        }else if(LocalDateTime.now().isBefore(goods.get(index).getStart())){
            System.out.println("该商品尚未开始秒杀");
        }else if (LocalDateTime.now().isAfter(goods.get(index).getEnd())){
            System.out.println("该商品已结束秒杀");
        }
        return false;
    }


    /*
    * 打印全部秒杀商品
    * */
    private static void printGoodsInfo() {
        System.out.println("-------------------------------------");
        for (int i = 0; i < goods.size(); i++) {
            System.out.print("秒杀开始时间："+goods.get(i).getStart().toLocalTime()+"\t");
//            System.out.println(LocalDateTime.now());
            System.out.println("秒杀结束时间："+goods.get(i).getEnd());
            if(LocalDateTime.now().isAfter(goods.get(i).getStart()) &&
                    LocalDateTime.now().isBefore(goods.get(i).getEnd())){
                System.out.println("正在秒杀");
            } else if (LocalDateTime.now().isBefore(goods.get(i).getStart())){
                System.out.println("即将开始");
            }else if (LocalDateTime.now().isAfter(goods.get(i).getEnd())){
                System.out.println("秒杀结束");
            }

            System.out.println("商品序号"+(i+1)+"："+"商品名称="+goods.get(i).getName()+",库存数量="+goods.get(i).getStock()
                    +",秒杀价格="+goods.get(i).getPrice()+"元");
            System.out.println("-------------------------------------");
        }
    }
}
